package com.syxx.oa.web.action;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import sun.org.mozilla.javascript.internal.IdScriptableObject;

import com.syxx.oa.domain.ApproveInfo_1;
import com.syxx.oa.domain.Department;
import com.syxx.oa.domain.Employee;
import com.syxx.oa.domain.PersonnelRequirement;
import com.syxx.oa.service.PersonnelRequirementService;
import com.syxx.oa.utils.StringUtils;
import com.test.jbpm.Pager;
import com.test.jbpm.domain.TroubleTicket;
import com.test.jbpm.web.action.AbtractTroubleTicketAction;

@Controller
@Scope("prototype")
public class PersonnelRequirementAction extends AbtractTroubleTicketAction {
	private PersonnelRequirementService personnelRequirementService;

	@Autowired
	public void setPersonnelRequirementService(
			PersonnelRequirementService personnelRequirementService) {
		this.personnelRequirementService = personnelRequirementService;
	}
	private PersonnelRequirement personnelReq;
	private Long[] _ids;
	
	
	public Long[] get_ids() {
		return _ids;
	}

	public void set_ids(Long[] _ids) {
		this._ids = _ids;
	}

	public PersonnelRequirement getPersonnelReq() {
		return personnelReq;
	}

	public void setPersonnelReq(PersonnelRequirement personnelReq) {
		this.personnelReq = personnelReq;
	}
	

	public String index() throws Exception {
		return SUCCESS;
	}
	public String approvingIndex() throws Exception {
		return SUCCESS;
	}
	public String approvedIndex() throws Exception {
		return SUCCESS;
	}
	
	public String list() throws Exception{
		 Pager<PersonnelRequirement> pager = personnelRequirementService.findMyPersonnelRequirementList(currentEmployee().getEmpId());
		 List<PersonnelRequirement> li = pager.getData();
		 JSONArray reList = new JSONArray();
		 if(li!=null && li.size()>0){
			 JSONObject json;
			 Map<String,Object> map;
			 for (PersonnelRequirement p : li) {
				 map = new HashMap<String, Object>();
				if(p.getDepartment() != null){
//					dep = new Department();
//					dep.setDepId(p.getDepartment().getDepId());
//					dep.setDepCode(p.getDepartment().getDepCode());
//					dep.setDepName(p.getDepartment().getDepName());
//					p.setDepartment(dep);
					map.put("depId", p.getDepartment().getDepId());
					map.put("depName", p.getDepartment().getDepName());
					p.setDepartment(null);
				}
				if(p.getCreateEmp() != null){
//					emp = new Employee();
//					emp.setEmpId(p.getCreateEmp().getEmpId());
//					emp.setEmpName(p.getCreateEmp().getEmpName());
//					p.setCreateEmp(emp);
					map.put("createEmpId",p.getCreateEmp().getEmpId());
					map.put("createEmpName", p.getCreateEmp().getEmpName());
					p.setCreateEmp(null);
				}
				if(p.getApproveInfoSet_() != null){
					p.setApproveInfoSet_(null);
				}
				
				json = StringUtils.bean2Json(p);
				json.put("createTime", StringUtils.getDatetime(p.getCreateTime()));
				json.putAll(map);
				
				reList.add(json);
			}
		 }
		 String str = StringUtils.returnPageList(pager,reList);
		 return ajaxOut(str);
	}
	

	public String add() throws Exception{
		personnelReq.setCreateEmp(currentEmployee());
		System.out.println(personnelReq.getPositionTime());
		personnelRequirementService.addOrUpdate(personnelReq);
		
		return ajaxOut("{success:true}");
	}
	
	public String dispatch() throws Exception {
		personnelRequirementService.dispatch(_ids);
		return ajaxOut();
	}
	
	public String approvingList() throws Exception {
		Pager<PersonnelRequirement> pager = personnelRequirementService.findApprovingPersonnelRequirementList(currentEmployee().getEmpId());
		List<PersonnelRequirement> li = pager.getData();
		 if(li!=null && li.size()>0){
			 Employee emp;
			 Department dep;
			 for (PersonnelRequirement p : li) {
				if(p.getDepartment() != null){
					dep = new Department();
					dep.setDepId(p.getDepartment().getDepId());
					dep.setDepCode(p.getDepartment().getDepCode());
					dep.setDepName(p.getDepartment().getDepName());
					p.setDepartment(dep);
				}
				if(p.getCreateEmp() != null){
					emp = new Employee();
					emp.setEmpId(p.getCreateEmp().getEmpId());
					emp.setEmpName(p.getCreateEmp().getEmpName());
					p.setCreateEmp(emp);
				}
				if(p.getApproveInfoSet_() != null){
					p.setApproveInfoSet_(null);
				}
			}
		 }
		 String str = StringUtils.returnPageList(pager);
		 return ajaxOut(str);
	}
	
	public String approvedList() throws Exception {
		Pager<PersonnelRequirement> pager = personnelRequirementService.findApprovedPersonnelRequirementList(currentEmployee().getEmpId());
		List<PersonnelRequirement> li = pager.getData();
		 if(li!=null && li.size()>0){
			 Employee emp;
			 Department dep;
			 for (PersonnelRequirement p : li) {
				if(p.getDepartment() != null){
					dep = new Department();
					dep.setDepId(p.getDepartment().getDepId());
					dep.setDepCode(p.getDepartment().getDepCode());
					dep.setDepName(p.getDepartment().getDepName());
					p.setDepartment(dep);
				}
				if(p.getCreateEmp() != null){
					emp = new Employee();
					emp.setEmpId(p.getCreateEmp().getEmpId());
					emp.setEmpName(p.getCreateEmp().getEmpName());
					p.setCreateEmp(emp);
				}
				if(p.getApproveInfoSet_() != null){
					p.setApproveInfoSet_(null);
				}
			}
		 }
		 String str = StringUtils.returnPageList(pager);
		 return ajaxOut(str);
	}
	
	public String edit() throws Exception {
		System.out.println("personnelReq.get_id()="+personnelReq.get_id());
		PersonnelRequirement p=new PersonnelRequirement();
		PersonnelRequirement point=new PersonnelRequirement();
		point = personnelRequirementService.findPersonnelRequirementById(personnelReq.get_id());
		
		
		p.set_id(personnelReq.get_id());
		p.setAge(point.getAge());
		p.setStationName(point.getStationName());
		p.setRecruitReason(point.getRecruitReason());
		p.setRecruitNum(point.getRecruitNum());
		p.setJobExplain(point.getJobExplain());
		p.setJobDescription(point.getJobDescription());
		p.setProReq(point.getProReq());
		p.setQuality(point.getQuality());
		p.setWorkExp(point.getWorkExp());
		p.setTecRequest(point.getTecRequest());
		long dep=point.getDepartment().getDepId();
		p.setSex(point.getSex());
		p.setPositionTime(point.getPositionTime());
		p.setDepMgrOpinion(point.getDepMgrOpinion());
		p.setLeaOption(point.getLeaOption());
		p.setHrOption(point.getHrOption());
		p.setHrLeaOption(point.getHrLeaOption());
		p.setViceGeMgrOption(point.getViceGeMgrOption());
		p.setViceGeMgrSign(point.getViceGeMgrSign());
		p.setGeMgrOption(point.getGeMgrOption());
		p.setGeMgrSign(point.getGeMgrSign());
		JSONObject jsonObject = JSONObject.fromObject(p);
		jsonObject.put("department.depId", dep);
		return ajaxOut("{success:true, data:" + jsonObject + "}");
	}
	
	public String del() throws Exception{
		System.out.println("进入del");
		System.out.println("_ids="+_ids[0]);
		personnelRequirementService.del(_ids);
		return ajaxOut();
	}
	

	
}
